﻿# Requires -Version 4.0

<#
.DESCRIPTION

.PARAMETER <Parameter Name>
.EXAMPLE
.INPUTS
.OUTPUTS
.RETURNVALUE
.EXAMPLE

.NOTES
Filename  : <Filename>
.NOTES
<Date and time, ISO8601> <Author> <History>

.LINK
Get-Help about_Comment_Based_Help
#>


Set-StrictMode -Version Latest


function Get-DriveLetterToPartition {
<#
.DESCRIPTION
Detect which drive letter is associated with a logical partition
.PARAMETER <Parameter Name>
.INPUTS
.OUTPUTS
.RETURNVALUE
.EXAMPLE
.LINK
MSDN Library: "WMI Tasks: Disks and File Systems"
(https://msdn.microsoft.com/en-us/library/aa394592)

.LINK
TechNet Library: "Get-CimInstance"
(https://technet.microsoft.com/en-us/library/jj590758.aspx)

.LINK
TechNet Library: "Get-CimAssociatedInstance"
(https://technet.microsoft.com/en-us/library/jj590756.aspx)
#>
[CmdletBinding()]
param(
<#	[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
	[String]$ParameterA#>
)
Begin {}

Process {
	#Get-CimInstance -ClassName Win32_LogicalDisk

    Get-CimInstance -ClassName Win32_DiskDrive |
    Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition | fl *
    Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk | 
    Select-Object -Property *
    

    # Get Local Disks
    Get-CimInstance -Query 'SELECT * FROM Win32_LogicalDisk WHERE DriveType=3' #-QueryDialect CQL
}

End {}

}  # MyFunction()

<#
Filename   : DiskInfo.ps1
Createdate : 2010-11-15
Author     : Niels Grove-Rasmussen
Description: Get information on disk konfiguration

MSDN > "WMI Tasks: Disks and File Systems"

Win32_DiskDrive
Win32_DiskPartition
Win32_LogicalDisk
Win32_Volume
Win32_DefragAnalysis
#>
<#
[string]$ComputerName = 'DNTEAMSPSQL01T.dn.lan'
#[string]$ComputerName = 'DNDBATEST01.dn.lan'
#[string]$ComputerName = 'DNMOSSSQL01.dn.lan'
#[string]$ComputerName = 'DNSQL02.dn.lan'  # Windows 2000

Write-Host ":: DiskInfo ($ComputerName)" -ForegroundColor Blue -BackgroundColor Yellow
$Disk = @()
Get-WmiObject -Query 'SELECT DeviceID FROM Win32_DiskDrive' -ComputerName $ComputerName |
ForEach-Object {  # WMI : Win32_DiskDrive
	$DriveID = $_.DeviceID
	Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$DriveID'} WHERE AssocClass = Win32_DiskDriveToDiskPartition" -ComputerName $ComputerName |
	ForEach-Object {  # WMI : Win32_DiskPartiton
		$StartingOffset = $_.StartingOffset
		Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($_.DeviceID)'} WHERE AssocClass = Win32_LogicalDiskToPartition" -ComputerName $ComputerName |
		Select-Object -Unique |
		ForEach-Object {  #WMI : Win32_LogicalDisk
			Get-WmiObject -Class Win32_Volume -ComputerName $ComputerName -Filter "DriveLetter='$($_.DeviceID)'" |
			ForEach-Object {
				$BlockSize = $_.BlockSize
				$_.DefragAnalysis().DefragAnalysis | ForEach-Object {
					$AverageFragmentsPerFile = $_.AverageFragmentsPerFile
					$FilePercentFragmentation = $_.FilePercentFragmentation
					$FreespacePercentFragmentation = $_.FreespacePercentFragmentation
					$MFTRecordCount = $_.MFTRecordCount
					$TotalFragmentedFiles = $_.TotalFragmentedFiles
					$TotalMFTFragments = $_.TotalMFTFragments
					$TotalPercentFragmentation = $_.TotalPercentFragmentation
				}
			}

			$Disk += New-Object PSObject -Property @{
				DiskID = $_.DeviceID
				Name = $_.VolumeName
				DriveID = $DriveID
				StartingOffset = $StartingOffset
				BlockSize = $BlockSize
				FreeSpace = $_.FreeSpace
				Size = $_.Size
				AverageFragmentsPerFile = $AverageFragmentsPerFile
				FilePercentFragmentation = $FilePercentFragmentation
				FreespacePercentFragmentation = $FreespacePercentFragmentation
				MFTRecordCount = $MFTRecordCount
				TotalFragmentedFiles = $TotalFragmentedFiles
				TotalPercentFragmentation = $TotalPercentFragmentation
			}
		}
	}
}
$Disk |
Select-Object DiskID,@{name="StartingOffset [KiB]";expression={$_.StartingOffset/1kb}},@{name="BlockSize [KiB]";expression={$_.BlockSize/1kb}},@{name="FreeSpace [GiB]";expression={$_.FreeSpace/1gb}},TotalPercentFragmentation |
Format-Table -AutoSize
#>
<#
"`n:: DiskDrive"
Get-WmiObject -Query "SELECT * FROM Win32_DiskDrive" -ComputerName $ComputerName | 
Select-Object DeviceID | 
Format-Table -AutoSize
#>
<#
"`n:: DiskPartition"
Get-WmiObject -Query "SELECT * FROM Win32_DiskPartition" -ComputerName $ComputerName | 
Select-Object BlockSize,StartingOffset,DeviceID | 
Format-Table -AutoSize
#>
<#
"`n:: LogicalDisk"
Get-WmiObject -Query "SELECT * FROM Win32_LogicalDisk" -ComputerName $ComputerName | 
Select-Object FreeSpace,Size,DeviceID | 
Format-Table -AutoSize
#>
<#
Write-Host "`n:: Volume" -ForegroundColor Blue -BackgroundColor Yellow
# NB: The WMI class Win32_Volume is not available on Windows XP and earlier
Get-WmiObject -Query "SELECT * FROM Win32_Volume" -ComputerName $ComputerName | 
Select-Object DriveLetter,Label,SerialNumber,BlockSize | 
Format-Table -AutoSize
#>
<#
Write-Host "`n:: DefragAnalysis (C:)" -ForegroundColor Blue -BackgroundColor Yellow
# NB: The WMI class Win32_Volume is not available on Windows XP and earlier
#   Reference: http://itknowledgeexchange.techtarget.com/powershell/defrag-analysis-part-2/
Get-WmiObject -Class Win32_Volume -ComputerName $ComputerName |
Where-Object { $_.DriveLetter -eq 'C:' } |
ForEach-Object {
	$_.DefragAnalysis().DefragAnalysis |
	Select-Object AverageFileSize,MFTRecordCount
	Format-Table -AutoSize
}
#>


<#
Filename   : DiskAlignment.ps1
Createdate : 2010-09-22
Author     : Niels Grove-Rasmussen
Description: Get Disk Alignment (Volume Alignment)
#>
<#
$wql = "SELECT DiskIndex,Index,StartingOffset FROM Win32_DiskPartition"
Get-WmiObject -Query $wql -ComputerName 'DNSQL09.dn.lan' | Select-Object DiskIndex,Index,@{Name='Offset (KB)';Expression={$_.StartingOffset / 1024}} | 
#Format-Table -AutoSize
Out-GridView
#>


<# Disk Size
Get-WMIObject Win32_LogicalDisk -filter 'DriveType=3' |
Select-Object SystemName,DeviceID,VolumeName,@{Name='Size (GB)';Expression={"{0:N1}" -f($_.size/1gb)}},@{Name='Freespace (GB)';Expression={"{0:N1}" -f($_.freespace/1gb)}} |
Out-GridView


# Freespace
Get-WmiObject -Query "SELECT FreeSpace, Size FROM Win32_LogicalDisk WHERE DeviceID = 'F:'" -ComputerName SRV9DNBDBM001.dn.lan |
Format-Table @{Name='Freespace (GB)';Expression={"{0:N1}" -f($_.FreeSpace/1gb)}}, @{Name='Size (GB)';Expression={"{0:N1}" -f($_.Size/1gb)}} -AutoSize
#>


###  INVOKE  ###
Get-DriveLetterToPartition
